//gen random weights
//get optimal policy

//get new optimal policy , add to set of policies
//--run for n times--
del=.1;
w=rand(ws,1);
w=w/norm(w,1);
wc=zeros(ws,101);
wc(:,1)=w;
pls1=cell();
vli1=cell();
pls2=cell();
vli2=cell();
vli3=cell();
qw1=cell();
qw2=cell();
for i=1:ngen+1
    gsize=ggsize(i);
    grid=ggrid(i).entries;
    pls1(i).entries=genpls2(w,1);
    [qw,vli]=genq(pls1(i).entries,w);
    vli1(i).entries=vli;
    qw1(i).entries=qw;
end
for ns=1:100
    wa=w;
    r1=floor(rand()*ws)+1;
    r2=floor(rand()*2)*2-1;
    //del2=.0095*(100-ns+1)+.05;
    //r2=r2*del2;
    r2=r2*del;
    wa(r1)=wa(r1)+r2;
    if wa(r1)<0
        r2=rand();
        if r2>.3
            wa(r1)=0;
        else
            wa(r1)=rand();
        end
    end
    wa=wa/norm(wa,1);
//    dr=0;
//    for i=1:ngen+1
//        gsize=ggsize(i);
//        grid=ggrid(i).entries;
//        [qwa,vli2(i).entries]=genq(pls1(i).entries,wa);
//        //qw2(i).entries=qwa;
//        //vli2(i).entries=vlia;
////        qw=zeros(qwa);
////        qw(:,:,1)=vli1(i).entries;
////        qw(:,:,2)=vli1(i).entries;
////        qw(:,:,3)=vli1(i).entries;
//        dr=dr+sum(vli1(i).entries(1,1)<vli2(i).entries(1,1));
////    qw1=zeros(qw2);
////    qw(:,:,1)=vli2;
////    qw(:,:,1)=vli2;
////    qw(:,:,1)=vli2;
////    for i=1:gsize
////        for j=1:3*gsize-1
////            if i==1
////                ra=zeros(2,1);
////                rb=zeros(2,1);
////                ra(1)=rw(i-1,j-1,0,w)+vli1(i,j+1);
////                rb(1)=rw(i-1,j-1,0,wa)+vli2(i,j+1);
////                ra(2)=rw(i-1,j-1,1,w)+vli1(i+1,j+1);
////                rb(2)=rw(i-1,j-1,1,wa)+vli2(i+1,j+1);
////            elseif i==gsize
////                ra=zeros(2,1);
////                rb=zeros(2,1);
////                ra(2)=rw(i-1,j-1,0,w)+vli1(i,j+1);
////                rb(2)=rw(i-1,j-1,0,wa)+vli2(i,j+1);
////                ra(1)=rw(i-1,j-1,-1,w)+vli1(i-1,j+1);
////                rb(1)=rw(i-1,j-1,-1,wa)+vli2(i-1,j+1);
////            else
////                ra=zeros(3,1);
////                rb=zeros(3,1);
////                ra(1)=rw(i-1,j-1,-1,w)+vli1(i-1,j+1);
////                rb(1)=rw(i-1,j-1,-1,wa)+vli2(i-1,j+1);
////                ra(2)=rw(i-1,j-1,0,w)+vli1(i,j+1);
////                rb(2)=rw(i-1,j-1,0,wa)+vli2(i,j+1);
////                ra(3)=rw(i-1,j-1,1,w)+vli1(i+1,j+1);
////                rb(3)=rw(i-1,j-1,1,wa)+vli2(i+1,j+1);
////            end
////            dr=dr+sum(ra<rb);
////        end
////    end
//    //dr=sum(qw<qw2);
//    end
    //printf("dr %d\n",dr);
    //if dr>0
        //calcprob
        pr=1;
        for i=1:ngen+1
            gsize=ggsize(i);
            grid=ggrid(i).entries;
            epls=gepls(i).entries;
            pls2(i).entries=genpls2(wa,1);
            [qw3,vli3(i).entries]=genq(pls2(i).entries,wa);
            qw2(i).entries=qw3;
            //pr=pr*exp(vli3(i).entries(1,1)-vli1(i).entries(1,1));
            //pr=pr*prbq(epls,qw3,qw1(i).entries);
            pth1=atos2(pls2(i).entries);
            pth2=atos2(pls1(i).entries);
            pr=pr*prbqb(epls,wa,w,pth1,pth2);
        end
//        [s,plo1]=atos2(pls1);
//        [s,plo2]=atos2(pls2);
//        er1=evalr(plo1);
//        er2=evalr(plo2);
//        pr=exp(er2*wa-er1*w);
        printf("pr %f\n",pr);
        pr=min(1,pr);
        r1=rand();
        if r1<pr
            pls1=pls2;
            vli1=vli3;
            qw1=qw2;
            w=wa;
        end
        //set w=wa,plsi etc
//    else
//        //qw1=genq(pls1,w);
//        //evals(pls1,w,qw1) etc
//        //[s,plo1]=atos2(pls1);
//        //[s,plo2]=atos2(pls2);
//        //er1=evalr(plo1);
//        //er2=evalr(plo2);
//        pr=1;
//        for i=1:ngen+1
//            pr=exp(vli2(i).entries(1,1)-vli1(i).entries(1,1));
//        end
//        printf("pr %f\n",pr);
//        pr=min(1,pr);
//        pr=pr*.1;
//        r1=rand();
//        if r1<pr
//            //pls1=pls2;
//            vli1=vli2;
//            w=wa;
//        end
        //calcprob
        //set only w=wa
    //end
    //w=w/norm(w,1);
    wc(:,ns+1)=w;
    printf("%d\n",ns);
end
